package dev.caobaoqi6040.backend.modules.ai.controller;

import dev.caobaoqi6040.backend.modules.ai.service.OllamaService;
import dev.caobaoqi6040.backend.modules.ai.service.OpenAIService;
import dev.caobaoqi6040.backend.modules.oss.domain.request.ObjectRequestVo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;

/**
 * OpenAIController
 *
 * @author caobaoqi6040
 * @since 2025/10/9 10:59
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/ai/open-ai")
public class OpenAIController {

	private final OpenAIService service;

	@GetMapping("/sample-chat")
	public String sampleChat(@RequestParam(value = "query") String query) {
		return service.sampleChat(query);
	}

	@GetMapping("/stream-chat")
	public Flux<String> streamChat(@RequestParam(value = "query") String query, HttpServletResponse response) {
		response.setCharacterEncoding(StandardCharsets.UTF_8.name());
		return service.streamChat(query);
	}

	@PostMapping("/image/text")
	public String image2text(@RequestBody ObjectRequestVo vo) throws MalformedURLException {
		return service.image2SampleText(vo.url());
	}

	@PostMapping("/image/stream")
	public String image2stream(@RequestBody ObjectRequestVo vo, HttpServletResponse response) throws MalformedURLException {
		response.setCharacterEncoding(StandardCharsets.UTF_8.name());
		return service.image2SampleText(vo.url());
	}
}
